package com.chance.cc.crawler.prod.command.job.domain.news.hupu.module;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/4/11 10:49
 * @Description
 *      虎扑
 **/
public class HupuModuleKeywordRealtimeCrawlerSchedulerJob extends NewsCommonScript {
    public static final String domain = "hupu";
    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = crawlerSchedulejob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("虎扑板块以关键词的形式传入每10分钟定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;

    }

    public static CrawlerJob crawlerSchedulejob(){
        String url = "https://www.hupu.com/";
        String site = "moduleKeyword";
        String siteBiz = "news-realtime";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(keyOrDateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(1,null))
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,siteBiz,"queue")))
                .releaseTime(System.currentTimeMillis())
                .httpHead("Cookie","bbs_2020=1; _dacevid3=3b16b26b.de34.159e.40d3.81c2315f14ac; Hm_lvt_c324100ace03a4c61826ef5494c44048=1614161750; __gads=ID=62f4ca940fdfe3e9:T=1614161751:S=ALNI_MZ4n8zaYcNhoEzUrqJfSCq7zlOxUA; smidV2=202102241816284dc4f610677b49f7c9ab822b474add6b0017ce92fcad1e2e0; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22177d389a4e9338-0d3d73f2e75944-393e5b09-1327104-177d389a4ea611%22%2C%22%24device_id%22%3A%22177d389a4e9338-0d3d73f2e75944-393e5b09-1327104-177d389a4ea611%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; _HUPUSSOID=40270922-a5e2-4705-aee9-c21a5378e059; _CLT=b0c2a05996d8b48b354e1fa4ddfc1fef; Hm_lvt_4fac77ceccb0cd4ad5ef1be46d740615=1618107355; csrfToken=oziuZJdJKi8Jbm3755YBeh52; Hm_lvt_df703c1d2273cc30ba452b4c15b16a0d=1618378740,1618378773,1618378782,1618379061; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22177d389a4e9338-0d3d73f2e75944-393e5b09-1327104-177d389a4ea611%22%2C%22%24device_id%22%3A%22177d389a4e9338-0d3d73f2e75944-393e5b09-1327104-177d389a4ea611%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; Hm_lvt_b241fb65ecc2ccf4e7e3b9601c7a50de=1618107355,1618379406; Hm_lpvt_b241fb65ecc2ccf4e7e3b9601c7a50de=1618379406; acw_tc=2f624a3e16183801828537372e7777d89db05e844c5ebef4771b411c029341; Hm_lpvt_df703c1d2273cc30ba452b4c15b16a0d=1618380486")
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,siteBiz,"comment","queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(1,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        crawlerRequestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://bbs.hupu.com/%s-postdate-1");

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("hupu_keyword",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domain+"/keys?site=nikeModule")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //每条帖子的结果
        String resultPostQueueName = "hupu_moduleKeyword_item_url";
        CrawlerQueueConfig crawlerPostQueueConfig = new CrawlerQueueConfig(
                resultPostQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);
        //每一条帖子结果同步
        CrawlerRequestRecord syncCrawlerPostRecord = CrawlerRequestRecord.builder()
                .startPageRequest("hupu_moduleKeyword_result", turnPageItem)
                .httpUrl("http://"+metaServiceIp+":"+metaServicePort+"/crawler/domain/common/api/v1/"+domain+"/search/results/sync?resultQueue=" + resultPostQueueName + "&site=moduleKeyword_item_url")
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteBiz,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,siteBiz,"queue")))
                .queueResultPipeline("redis",crawlerPostQueueConfig)
//                .fileResultPipeline(null, "/data/chance_crawler_test/data/acfun/acfun_searchKw_video_realtime.log", false)
                .kafkaResultPipeline("kafka",kafkaTopic,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .supportRecord(syncCrawlerPostRecord)
                .build();
    }



}
